package com.lichuan.zhaoshangdemo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lichuan.zhaoshangdemo.common.Result;
import com.lichuan.zhaoshangdemo.entity.ButtonItems;
import com.lichuan.zhaoshangdemo.entity.MenuItems;
import com.lichuan.zhaoshangdemo.mapper.ButtonItemsMapper;
import com.lichuan.zhaoshangdemo.mapper.MenuItemsMapper;
import com.lichuan.zhaoshangdemo.service.MenuItemsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MenuItemsServiceImpl  extends ServiceImpl<MenuItemsMapper, MenuItems> implements MenuItemsService {

    @Autowired
    private ButtonItemsMapper buttonItemsMapper;

    @Override
    public Result<List<MenuItems>> get() {
        LambdaQueryWrapper<MenuItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MenuItems::getIstatus, 1);

        List<MenuItems> allMenus = this.list(queryWrapper);

        // 构建树形结构
        List<MenuItems> treeMenus = buildMenuTree(allMenus, "b4869d93-e730-448b-bed5-9082d028a5b3");

        // 为叶子节点菜单项附加按钮项
        attachButtonItems(treeMenus, allMenus);

        return Result.success(treeMenus);
    }

    /**
     * 为叶子节点附加按钮
     * @param menus 树形菜单
     * @param allMenus 所有菜单项
     */
    private void attachButtonItems(List<MenuItems> menus, List<MenuItems> allMenus) {
        for (MenuItems menu : menus) {
            // 判断是否为叶子节点
            if (isLeafNode(menu, allMenus)) {
                // 为叶子节点查询并设置按钮
                List<ButtonItems> buttonItems = getButtonItems(menu.getUidmenuid());
                menu.setButtons(buttonItems);
            }

            // 递归处理子菜单
            if (menu.getChildren() != null && !menu.getChildren().isEmpty()) {
                attachButtonItems(menu.getChildren(), allMenus);
            }
        }
    }

    /**
     * 获取菜单关联按钮
     * @param uidmenuid 菜单ID
     * @return 按钮列表
     */
    public List<ButtonItems> getButtonItems(String uidmenuid) {
        LambdaQueryWrapper<ButtonItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ButtonItems::getIstatus, 1)
                .eq(ButtonItems::getUidmenuid, uidmenuid);
        List<ButtonItems> result = buttonItemsMapper.selectList(queryWrapper);
        return result != null ? result : new ArrayList<>();
    }

    /**
     * 构建菜单树
     */
    public List<MenuItems> buildMenuTree(List<MenuItems> allMenus, String parentId) {
        List<MenuItems> tree = new ArrayList<>();

        // 筛选出指定父级ID的菜单项
        List<MenuItems> children = allMenus.stream()
                .filter(menu -> parentId.equals(menu.getUidparentmenuid()))
                .collect(Collectors.toList());

        // 递归构建子树
        for (MenuItems menu : children) {
            List<MenuItems> childTree = buildMenuTree(allMenus, menu.getUidmenuid());
            menu.setChildren(childTree);
            tree.add(menu);
        }

        return tree;
    }

    /**
     * 判断是否为叶子节点（最后一级菜单）
     * @param menuItem 菜单项
     * @param allMenus 所有菜单项
     * @return 是否为叶子节点
     */
    private boolean isLeafNode(MenuItems menuItem, List<MenuItems> allMenus) {
        // 检查是否有子菜单
        for (MenuItems menu : allMenus) {
            if (menu.getUidparentmenuid() != null &&
                menu.getUidparentmenuid().equals(menuItem.getUidmenuid())) {
                return false; // 找到子菜单，不是叶子节点
            }
        }
        return true; // 没有子菜单，是叶子节点
    }
}
